import { observable, action } from 'mobx';
import * as Apis from "../../api/customerApis";
import { Toast, Bubble, Tools } from 'weaver-mobile';
import { Form } from 'weaver-mobile-page';
import * as adapter from '../../constants/adapter';
const getLabel = Tools.getLabel;
export default class ChanceFormStore {
    comObj = {};
    params = {};
    formData = []; // 更新前的原始数据
    @observable id = "";
    @observable customerId = "";
    @observable initResponsed = false; // 是否已经得到初始化数据的响应
    @observable initSuccessed = false; // 初始化数据是否获取成功
    @observable initFailedMsg = "";    // 初始化处理失败的原因
    @observable form = {};
    @observable saveLoading = false;
    @observable buttonProps = {loading:false,disabled:false};

    @action
    doNew = (comObj, params) => {
        this.comObj = comObj;
        this.params = params;
    }

    @action
    doInit = () => {
        this.form = new Form.Store();
        let params = this.params;
        this.id = params.chanceId;
        this.customerId = params.customerId;
        Apis.getChanceForm(params).then(data => {
            this.doInitCallBack(params, data);
        });
    }

    @action
    doInitCallBack = (params, data) => {
        this.initResponsed = true;
        this.canEdit = data.canEdit;
        if (data.status == "success") {
            this.initSuccessed = true;
            this.formData = data.datas.formItemGroups;
            const currentPath = this.comObj.props.location.pathname;
            this.form.initForm(data.datas.formItemGroups, currentPath);
        } else {
            this.initSuccessed = false;
            this.initFailedMsg = data.msgcode;
        }
    }

    @action
    doClear = () => {
        this.initResponsed = false;
        this.initSuccessed = false;
        this.initFailedMsg = "";
    }

    @action
    doSave = () => {
        this.buttonProps={loading:true,disabled:true};
        const operation = this.params.operation;
        if (this.form.doValidate().pass) {
            if (operation == "add") {
                const params = {operation: "add", ...this.form.formParams };
                Apis.createChance(params).then(data => {
                    if (data.status == "success") {
                        this.buttonProps={loading:false,disabled:false};
                        window.crmGlobal && window.crmGlobal.doSearch();                     
                        Bubble.success(getLabel('83551',"保存成功"), 1, {backgroundColor:adapter.BUBBLE_BGCOLOR}, 2, ()=>{
                            this.comObj.props.history.go(-1);
                            if(this.comObj.props.cardStore){
                                this.comObj.props.cardStore.tabChange("chance");
                            }
                        });
                    } else {
                        Toast.fail(data.msgcode, 1);
                    }
                });
            } else {

            }
        }else{
            this.buttonProps = {loading:false,disabled:false};
        }
    }

    @action //修改商机来源的浏览按钮条件
    doChangeFormCondition=(domkey,param)=>{
        if(this.form.isInit){
            this.form.conditioninfo.forEach((group) => {
                group.items.forEach((field) => {
                    if(field.domkey.join('_')===domkey){
                        if(param) {
                            field.browserConditionParam.dataParams.CustomerID=param;
                        }         
                    }
                })
            })
        }
    }
    @action
    groupToggle = (...arg) => {
        const title = arg[0];
        const status = arg[1];
        this.form.conditioninfo = this.form.conditioninfo.map(c => ({
            ...c,
            ...(c.title === title ? { show: status } : {}),
        }));
    }
}